#include "ObPipe.h"
#include "ObPlayer.h"
#include "Collision.h"
#include "MapScene.h"
#include "ObPlant.h"
#include "D3DInput.h"

IDirect3DTexture9* ObPipe::mTex		= 0;
IDirect3DTexture9* ObPipe::mTreeTex	= 0;

ObPipe::ObPipe(){
	mID		= 0;
	mInfo	= 0;
	mWidth	= 32.0f;
	mHeight = 16.0f;
	mCenter = D3DXVECTOR3(mWidth/2, mHeight/2, 0.0f);
	mSpritePos.x = 10;
	mSpritePos.y = 20;

	hasTree = 1;
	inside = 0;
	outside = 0;
	isCreated = false;
	mMovingTo = 0;
}

ObPipe::ObPipe(PIPE nid, int info){

	setId(nid);
	setInfo(info);

	isCreated = false;
	mMovingTo = 0;
}

ObPipe::~ObPipe(){

}

bool ObPipe::setId(int nid){
	if(checkType(nid)){
		mID = nid;
		if(mID == Up || mID == Down || mID == MidVer){
			mWidth = 32.0f; mHeight = 16.0f;
			if(mID == Up){
				mSpritePos.x = 30;
				mSpritePos.y = 50;
			}else if(mID == MidVer){
				mSpritePos.x = 30;
				mSpritePos.y = 70;
			}else if(mID == Down){
				mSpritePos.x = 30;
				mSpritePos.y = 90;
			}
		}else{
			mWidth = 16.0f; mHeight = 32.0f;
			if(mID == Left){
				mSpritePos.x = 10;
				mSpritePos.y = 20;
			}else if(mID == MidHor){
				mSpritePos.x = 30;
				mSpritePos.y = 20;
			}else if(mID == Right){
				mSpritePos.x = 50;
				mSpritePos.y = 20;
			}
		}
		mCenter = D3DXVECTOR3(mWidth/2, mHeight/2, 0.0f);
		return true;
	}else{
		return false;
	}
}

bool ObPipe::setInfo(int ninfo){
	if(checkInfo(ninfo)){
		mInfo = ninfo;
		hasTree = ninfo/100;
		int left = ninfo - hasTree*100;
		inside = left/10;
		outside = left - inside*10;
		return true;
	}else{
		return false;
	}
}

bool ObPipe::checkType(int obID){
	if(obID<Up || obID>MidVer){
		return false;
	}else{
		return true;
	}
}

bool ObPipe::checkInfo(int ninfo){
	if(ninfo >=100 && ninfo <300){
		return true;
	}else{
		return false;
	}
}

void ObPipe::collidedWith(ObObject* object, CollisionRS rs){
	switch (object->getId())
	{
	case ObPlayer::MARIO:
		if(ObPlayer* player  = dynamic_cast<ObPlayer*>(object)){
			if(inside != 0 && hasTree == 1){
				bool beable = false;
				switch (mID)
				{
				case Up:
					if(gInput->keyDown(DIK_DOWNARROW) && rs.mState == CollisionState::Top){
						for(unsigned int i = 0; i < gMapScene->mMap->getChosedMap().size(); i++){
							if(checkType(gMapScene->mMap->getChosedMap()[i]->getId())){
								if(ObPipe* pipe  = dynamic_cast<ObPipe*>(gMapScene->mMap->getChosedMap()[i])){
									if(pipe->outside == inside && pipe->hasTree == 1){
										beable = true;
										mMovingTo = pipe;
									}
								}
							}
						}
					}
					break;
				case Down:
					if(gInput->keyDown(DIK_UPARROW)  && rs.mState == CollisionState::Bottom){
						for(unsigned int i = 0; i < gMapScene->mMap->getChosedMap().size(); i++){
							if(checkType(gMapScene->mMap->getChosedMap()[i]->getId())){
								if(ObPipe* pipe  = dynamic_cast<ObPipe*>(gMapScene->mMap->getChosedMap()[i])){
									if(pipe->outside == inside && pipe->hasTree == 1){
										beable = true;
										mMovingTo = pipe;
									}
								}
							}
						}
					}
					break;
				case Left:
					if(gInput->keyDown(DIK_RIGHTARROW)  && rs.mState == CollisionState::Left){
						for(unsigned int i = 0; i < gMapScene->mMap->getChosedMap().size(); i++){
							if(checkType(gMapScene->mMap->getChosedMap()[i]->getId())){
								if(ObPipe* pipe  = dynamic_cast<ObPipe*>(gMapScene->mMap->getChosedMap()[i])){
									if(pipe->outside == inside && pipe->hasTree == 1){
										beable = true;
										mMovingTo = pipe;
									}
								}
							}
						}
					}
					break;
				case Right:
					if(gInput->keyDown(DIK_LEFTARROW) && rs.mState == CollisionState::Right){
						for(unsigned int i = 0; i < gMapScene->mMap->getChosedMap().size(); i++){
							if(checkType(gMapScene->mMap->getChosedMap()[i]->getId())){
								if(ObPipe* pipe  = dynamic_cast<ObPipe*>(gMapScene->mMap->getChosedMap()[i])){
									if(pipe->outside == inside && pipe->hasTree == 1){
										beable = true;
										mMovingTo = pipe;
									}
								}
							}
						}
					}
					break;
				default:
					break;
				}

				if(beable){
					float delay = 0.5f;
					D3DXVECTOR3 velo = D3DXVECTOR3(0.0f,0.0f,0.0f);
					D3DXVECTOR3 veloout = D3DXVECTOR3(0.0f,0.0f,0.0f);
					switch (mID)
					{
					case Up:
						velo.y = -25.0f;
						break;
					case Down:
						velo.y = 25.0f;
						break;
					case Left:
						velo.x = 25.0f;
						break;
					case Right:
						velo.x = -25.0f;
						break;
					default:
						break;
					}

					switch (mMovingTo->getId())
					{
					case Up:
						veloout.y = 25.0f;
						break;
					case Down:
						veloout.y = -25.0f;
						break;
					case Left:
						veloout.x = -25.0f;
						break;
					case Right:
						veloout.x = 25.0f;
						break;
					default:
						break;
					}

					player->transportByPile(mMovingTo->getPos().x, mMovingTo->getPos().y,delay,velo,veloout);
				}
			}
		}
		break;
	default:
		break;
	}
}

void ObPipe::update(float dt){
	if(hasTree == 2 && isCreated == false){
		isCreated = true;
		ObPlant::STATUS temp;
		switch (mID)
		{
		case Up:
			temp = ObPlant::TOP;
			break;
		case Down:
			temp = ObPlant::DOWN;
			break;
		case Left:
			temp = ObPlant::LEFT;
			break;
		case Right:
			temp = ObPlant::RIGHT;
			break;
		default:
			return;
		}
		gMapScene->mNewObject.push_back(new ObPlant(this,mTreeTex,mPos.x,mPos.y, temp));
	}
}

void ObPipe::render(ID3DXSprite* mSprite){
	if(mID == 0) return;
	if(mTex == 0) return;
	if(mNeedDispose) return;

	HR(gDevice->SetRenderState(D3DRS_ALPHATESTENABLE,true));
	D3DXMATRIX S;
	D3DXMatrixScaling(&S,1.0f,-1.0f,1.0f);
	D3DXMATRIX R;
	D3DXMatrixRotationZ(&R, mRotation);
	D3DXMATRIX T;
	D3DXMatrixTranslation(&T,mPos.x - gCamera->getPos().x,mPos.y - gCamera->getPos().y,0);
	mSprite->SetTransform(&(S*R*T));
	RECT rect = calRenderRECT(mSpritePos,mWidth,mHeight);
	mSprite->Draw(mTex,&rect,&mCenter,0,D3DCOLOR_XRGB(255, 255, 255));
	mSprite->Flush();
	HR(gDevice->SetRenderState(D3DRS_ALPHATESTENABLE,false));
}

bool ObPipe::loadTexture(){
	if(mTex == 0){
		HR(D3DXCreateTextureFromFileEx(
			gDevice,
			"Sprites/PipeH.png",
			D3DX_DEFAULT_NONPOW2,
			D3DX_DEFAULT_NONPOW2,
			0,
			0,
			D3DFMT_UNKNOWN,
			D3DPOOL_MANAGED,
			D3DX_DEFAULT,
			D3DX_DEFAULT,
			D3DCOLOR_XRGB(0,0,0),
			NULL,
			NULL,
			&mTex));
	}else{
		return false;
	}

	if(mTreeTex == 0){
		HR(D3DXCreateTextureFromFileEx(
			gDevice,
			"Sprites/Plant.png",
			D3DX_DEFAULT_NONPOW2,
			D3DX_DEFAULT_NONPOW2,
			0,
			0,
			D3DFMT_UNKNOWN,
			D3DPOOL_MANAGED,
			D3DX_DEFAULT,
			D3DX_DEFAULT,
			D3DCOLOR_XRGB(0,0,0),
			NULL,
			NULL,
			&mTreeTex));
	}else{
		return false;
	}
	return true;
}

bool ObPipe::releaseTexture(){
	if(mTex!=0){
		ReleaseCOM(mTex);
	}else{
		return false;
	}

	if(mTreeTex!=0){
		ReleaseCOM(mTreeTex);
	}else{
		return false;
	}
	return true;
}